<?php
header('Content-type: application/xls');
header("Content-Disposition:attachment;filename=ketquaBHLĐ.xls");
?>
<?php
    echo $others->conghoa($tendv ['Zdv'] ['tendvcha'] ,$tendv ['Zdv'] ['tendv'],3,4,'Kết quả bán hàng lưu động'); 
    echo '<h3>Từ: '.date('d-m-Y',strtotime($ngaybdbhld)).' đến '.date('d-m-Y',strtotime($ngayktbhld)).'</h3>'
?>
<?php if(!empty($hanghoabhlds) && !empty($chitieungaybhlds)) {?>
<?php
 $tongdk = $hanghoabhlds;
			$tongkq = $hanghoabhlds; 
?>
		<table id ='tables'  border="1">
				<tr>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">STT</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Trung Tâm</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Nhân Viên</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Đội</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Địa Điểm</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Ngày</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Thành viên tham gia</th>
				<th style="background: #d5d5d5; font-weight: bold" colspan=<?php echo count($hanghoabhlds)*3?>>Kết Quả</th>
				<th rowspan=3 style="background: #d5d5d5; font-weight: bold">Ghi chú</th>
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th colspan=3><?php echo $valhh; $tongdk[$keyhh]=0; $tongkq[$keyhh]=0; ?></th>
				<?php endforeach;?>
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th>Chỉ Tiêu</th>
				<th>Kết quả</th>
				<th>% Hoàn Thành </th>
				<?php endforeach;?>
				</tr>
				<?php
					$i=0; 
					 foreach ($chitieungaybhlds as $chitieungay):
					  $i++;
				?>
				<tr>
				<td><?php echo $i?></td>
				<td><?php echo $chitieungay['Nhanvien']['Donvi']['madv']?></td>
				<td><?php echo $chitieungay['Nhanvien']['tennv']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['tendoi']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['diadiem']?></td>
				<td><?php echo date('d-m-Y',strtotime($chitieungay['Kehoachbhld']['thoigian']))?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['nhansu']?></td>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<td>
				<?php
					$cosl = 0;
					$cokq = 0;
					$pt = 0;
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['soluong'];
				 			$tongdk[$keyhh] +=$ctngay['soluong'];
				 			$cosl = 1;
				 			break;
						}	
					}
					if($cosl==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
				 			echo $ctngay['ketqua'];
				 			$tongkq[$keyhh] +=$ctngay['ketqua'];
				 			$cokq=1;
				 			break;
						}	
					}
					if($cokq==0)
						echo 0;
				?>
				</td>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
						{
							$num=(empty($ctngay['soluong'])? 0 :$ctngay['ketqua']*100/$ctngay['soluong']);
				 			echo number_format(round($num,2),0,',','.').'%';
				 			$pt = 1;
				 			break;
						}	
					}
					if($pt==0)
						echo "0%";
				?>
				</td>
				<?php endforeach;?>
				<td><?php echo $chitieungay['Kehoachbhld']['ghichu']?></td>
				</tr>
				<?php 
                endforeach;?> 
                <tr ><td colspan=7 style="text-align:center;"> <b>Tổng</b> </td>
                <?php
					foreach ($hanghoabhlds as $keyh=>$valh)
					{?>
				 		<td><?php echo $tongdk[$keyh];?></td>	
				 		<td><?php echo $tongkq[$keyh];?></td>
				 		<td><?php echo number_format((round($tongdk[$keyh]==0?0:$tongkq[$keyh]*100/$tongdk[$keyh],2)),0,',','.').'%';?></td>
					<?php }?>
                </tr>
				</table>
					<?php }
					else
					{
					?>
					<h3>Không có dữ liệu</h3>
					<?php }?>